import random
import time
from cluster_qkd_manual_cluster import MultiClusterQKDNetwork
import matplotlib.pyplot as plt
from log import clearContent

compareTxt = "compare.txt"
clusterNum = 200

start_time = time.time()
qkdn = MultiClusterQKDNetwork(
    [f"/Users/xusun/Desktop/Python/qkd-oblivious-python/template10/cluster{i}.txt" for i in range(1, 10)],
    clusterNum
)

end_time = time.time()
print(f"读簇：{end_time - start_time}s")

# qkdn.showClusterTopo()

clearContent("compare.txt")

for _ in range(20):
    end_time = time.time()
    qkdn.clearLinkLoad()
    for i in range(1000):
        _startClusterID = random.randint(1, clusterNum)
        _endClusterID = random.randint(1, clusterNum)
        qkdn.requestFlow(
            startClusterID = _startClusterID, 
            startNodeID = random.randint(1, qkdn.clusters[_startClusterID-1].nodeNum), 
            endClusterID = _endClusterID, 
            endNodeID = random.randint(1, qkdn.clusters[_endClusterID-1].nodeNum), 
            flow = random.randint(1, 50)
        )
    with open(compareTxt, "a") as file:
            file.write(f"{qkdn.maxAlpha} {qkdn.maxCompareAlpha}\n")
    run_time = time.time()
    print(f"对比：{run_time - end_time}s")

data = []
with open(compareTxt, "r") as file:
    for line in file:
        values = line.strip().split()
        data.append([float(values[0]), float(values[1])])
x = list(range(1, len(data) + 1))
y1 = [item[0] for item in data]
y2 = [item[1] for item in data]
plt.plot(x, y1, label='Cplex')  # 第一条折线，对应数据中的第一列
plt.plot(x, y2, label='Spf')  # 第二条折线，对应数据中的第二列
plt.title('Compare：1000 Flows')
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.legend()
plt.show()
